package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.DeviceService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 设备转发管理
 */
@Controller
@RequestMapping(value = "backend/forward")
public class DeviceForwardController extends CommonController {

    @Autowired
    DeviceService deviceService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/forward/list";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, Integer companyId, Integer deviceId, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, deviceService.pageByParams(companyId, deviceId, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer deviceId, String address, String port) {
        return deviceService.saveForward(request, deviceId, address, port);
    }

    @RequestMapping("/batchSave")
    @ResponseBody
    public Map<String, Object> batchSave(HttpServletRequest request, String address, String port) {
        return deviceService.batchSaveForward(request, address, port);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(HttpServletRequest request, Integer id) {
        return deviceService.delForward(request, id);
    }
}